package easemob.chatuidemo.utils;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.UserInfo;
import com.wolfalpha.utron.utils.*;

import java.util.Map;

import easemob.applib.controller.HXSDKHelper;
import easemob.chatuidemo.DemoHXSDKHelper;
import easemob.chatuidemo.domain.User;

public class UserUtils {
    /**
     * @param username
     * @return
     */
    public static User getUserInfo(String username) {
        Map<String, User> m = ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getContactList();
        Log.i("UserUtils", m.size() + "");
        User user = m.get(username);
        return user;
    }

    /**
     * 设置用户头像
     *
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView) {
        User user = getUserInfo(username);
        if (user == null) {
            return;
        }
//        ImageLoader imageLoader = new ImageLoader(context);
//        imageLoader.DisplayImage(user.getAvatar(), imageView);

        Picasso.with(context)
                .load(user.getAvatar())
                .placeholder(R.mipmap.head_default)
                .into(imageView);
    }

    /**
     * 设置当前用户头像
     */
    public static void setCurrentUserAvatar(Context context, ImageView imageView) {
        UserInfo user = AppContext.getInstance().getUserInfo();
        if (user == null) {
            return;
        }
        Log.i("setCurrentUserAvatar", user.getHead_pic());
        if (com.wolfalpha.utron.utils.ImageUtils.getBitmap(AppContext.getInstance(), "head_pic.jpg") != null) {
            imageView.setImageBitmap(com.wolfalpha.utron.utils.ImageUtils.getBitmap(AppContext.getInstance(), "head_pic.jpg"));
        } else {
            Picasso.with(context)
                    .load(user.getHead_pic())
                    .placeholder(R.mipmap.head_default)
                    .into(imageView);
        }
    }

    /**
     * 设置用户昵称
     */
    public static void setUserNick(String username, TextView textView) {
        User user = getUserInfo(username);
        if (user != null) {
            textView.setText(user.getNick());
        } else {
            textView.setText("未知");
        }
    }

    /**
     * 设置当前用户昵称
     */
    public static void setCurrentUserNick(TextView textView) {
        UserInfo user = AppContext.getInstance().getUserInfo();
        if (user == null) {
            return;
        }
        if (textView != null) {
            textView.setText(user.getNickname());
        }
    }

    /**
     * 保存或更新某个用户
     *
     * @param user
     */
    public static void saveUserInfo(User newUser) {
        if (newUser == null || newUser.getUsername() == null) {
            return;
        }
        ((DemoHXSDKHelper) HXSDKHelper.getInstance()).saveContact(newUser);
    }

}
